package com.leinao.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import com.leinao.constant.Constant;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.util.Auth;

/**
 * 七牛上传工具类
 * @author  chenhw
 */
public class QiNiuUtil {

	private final static String ACCESSKEY = "pNAjs3ZfCKxCw2m3y_FEQfDDyC60zyBptMhwEmhZ";
	private final static String SECRETKEY = "Uwn3wa7gnChnSp8OQzu23YgpqRtJPErMUuI1PWOE";
	private final static String BUCKET_IMG = "test";
	private final static String BUCKET_VIDEO = "videotest";

	private QiNiuUtil(){}
	
	private static final QiNiuUtil QI_NIU_UTIL = new QiNiuUtil();
	
	public static QiNiuUtil getInstance(){
		return QI_NIU_UTIL;
	}


	/**
	 * 上传图片资源
	 * @param obj 图片的数据流
	 * @param fileName 文件名
	 * @return 1：上传成功 0：失败
	 */
	public int uploadFile(Object obj,String fileName){
		return uploadFile(obj,fileName,BUCKET_IMG);
	}

	/**
	 * 上传视频到七牛服务器
	 * @param obj	上传的视频资源
	 * @param fileName	文件名称（按业务规则生成文件名。需带拓展名）
	 * @return	1：上传成功 0：失败
	 */
	public int uploadVideo(Object obj,String fileName){
		return uploadFile(obj,fileName, BUCKET_VIDEO);
	}


	/**
	 * 上传文件到七牛服务器
	 * @param obj	支持上传类型1.File文件  2.InputStream 3.图片路径 4.视频
	 * @param fileName	文件名称（按业务规则生成文件名。需带拓展名）
	 * @param bucket 存储空间
	 * @return	1：上传成功 0：失败
	 */
	private int uploadFile(Object obj,String fileName,String bucket){
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		String upToken = auth.uploadToken(bucket);
		try {
			Response response = null;
			if(obj instanceof File){
				response = uploadManager.put((File)obj, fileName, upToken);
			}else if(obj instanceof String){
				response = uploadManager.put((String)obj, fileName, upToken);
			}else if(obj instanceof InputStream){
				InputStream inStream = (InputStream)obj;
				ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
				//buff用于存放循环读取的临时数据
				byte[] buff = new byte[100];
				int rc = 0; 
				while ((rc = inStream.read(buff, 0, 100)) > 0) { 
					swapStream.write(buff, 0, rc); 
				}
				//in_b为转换之后的结果
				byte[] imgByte = swapStream.toByteArray();
				response = uploadManager.put(imgByte, fileName, upToken);
			}else{
				return -1;
			}
			
		    //解析上传成功的结果
		    if(response.statusCode == Constant.SUCCESS_CODE){
		    	System.out.println("upload success");
		    	return 1;
		    }else{
		    	System.out.println("upload filed");
		    	return 0;
		    }
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    System.err.println(r.toString());
		    try {
		        System.err.println(r.bodyString());
		    } catch (QiniuException ex2) {
		        //ignore
		    }
		    return 0;
		} catch (Exception e) {
			System.err.println("上传失败");
			return 0;
		}
	}


	
	/**
	 * 批量删除七牛服务器文件 目前就删除图片空间的
	 * @param fileNames	string数组 传入文件名即可
	 */
	public void delFiles(String[] fileNames){
		
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		try {
		    //单次批量请求的文件数量不得超过1000
		    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
		    batchOperations.addDeleteOp(BUCKET_IMG, fileNames);
		    Response response = bucketManager.batch(batchOperations);
		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
		    for (int i = 0; i < fileNames.length; i++) {
		        BatchStatus status = batchStatusList[i];
		        String key = fileNames[i];
		        System.out.print(key + "\t");
		        if (status.code == 200) {
		            System.out.println("delete success");
		        } else {
		            System.out.println(status.data.error);
		        }
		    }
		} catch (QiniuException ex) {
		    System.err.println(ex.response.toString());
		}
	}
		
}
